// Add a request interceptor
import axios from 'axios'
import { ElMessage } from 'element-plus'
import { useUserStore } from '@/stores/user.ts'
//配置基础url、超时时间、post请求头
axios.defaults.baseURL = 'http://localhost:8101'
axios.defaults.timeout = 5000
axios.defaults.headers.post['Content-Type'] = 'application/json'
axios.defaults.withCredentials = true

// Add a response interceptor
axios.interceptors.response.use(
  function (response) {
    if (response.data.code === 40100){
      ElMessage.error("未登录！请等候进行操作")
      useUserStore().$reset()
    }
    console.log('响应', response)
    // Any status code that lie within the range of 2xx cause this function to trigger
    // Do something with response data
    return response
  },
  function (error) {
    // Any status codes that falls outside the range of 2xx cause this function to trigger
    // Do something with response error
    return Promise.reject(error)
  },
)
